package com.wlit.ui;

import com.alibaba.fastjson.JSONObject;
import com.caysn.autoreplyprint.AutoReplyPrint;
import com.wlit.print.LabelPrinter;
import com.wlit.print.PrintConfig;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PrintDialog extends JDialog {
    private JComboBox<String> portList; // 串口列表
    private JLabel portLabel; // 串口标签
    private JButton openButton, closeButton, testButton, saveButton; // 按钮
    private JTextArea logTextArea; // 文本区域显示日志

    public PrintDialog(JFrame owner) {
        super(owner, "打印机串口配置", true); // 设置模态对话框
        initComponents();
        pack();
        setLocationRelativeTo(owner);
    }

    private void initComponents() {
        String comName = PrintConfig.getInstance().getComName();
        setLayout(new BorderLayout());
        // 创建串口标签
        portLabel = new JLabel("选择串口：");

        // 创建串口列表
        portList = new JComboBox<>();

        java.util.List<String> thePorts = new java.util.ArrayList<>();
        String[] port_list = AutoReplyPrint.CP_Port_EnumCom_Helper.EnumCom();
        for(String port:port_list){
            thePorts.add(port);
        }
        //让默认选中的串口为第一个位置
        if (comName != null && !comName.isEmpty()) {
            portList.addItem(comName);
            thePorts.remove(comName);
        }
        for(String port:thePorts){
            portList.addItem(port);
        }
        // 创建按钮
        openButton = new JButton("打开打印机");
        closeButton = new JButton("关闭打印机");
        testButton = new JButton("测试打印机");
        saveButton = new JButton("保存打印机配置");

        // 创建日志文本区域
        logTextArea = new JTextArea(10, 30);
        JScrollPane scrollPane = new JScrollPane(logTextArea);

        // 创建一个面板用于放置串口标签和串口列表
        JPanel portPanel = new JPanel(new FlowLayout());
        portPanel.add(portLabel);
        portPanel.add(portList);

        // 添加组件到对话框
        add(portPanel, BorderLayout.NORTH);
        add(new JPanel(new FlowLayout()) {{
            add(openButton);
            add(closeButton);
            add(testButton);
            add(saveButton);
        }}, BorderLayout.CENTER);
        add(scrollPane, BorderLayout.SOUTH);

        // 为按钮添加事件监听器
        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String portName = (String) portList.getSelectedItem();
                LabelPrinter.getInstance().open(portName);
                if (LabelPrinter.getInstance().isOpen()) {
                    log("打印机串口打开成功");
                    openButton.setEnabled(false);
                    closeButton.setEnabled(true);
                } else {
                    log("打印机串口打开失败");
                    openButton.setEnabled(true);
                    closeButton.setEnabled(false);
                }
                // 打开串口的逻辑
            }
        });

        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LabelPrinter.getInstance().close();
                openButton.setEnabled(true);
                closeButton.setEnabled(false);
                log("打印机串口关闭");
                // 关闭串口的逻辑
            }
        });

        testButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("bianhao", "WHP20240904010");
                jsonObject.put("name", "2-氨基苯砷酸");
                jsonObject.put("room", "教学楼-501室");
                jsonObject.put("pure", "99.999999%");
                jsonObject.put("owner", "黄朝晖");
                LabelPrinter.getInstance().print(jsonObject);
            }
        });

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 保存配置的逻辑
                String portName = (String)portList.getSelectedItem();
                if (LabelPrinter.getInstance().isOpen()) {
                    PrintConfig.getInstance().save(portName);
                    JOptionPane.showMessageDialog(PrintDialog.this, "打印机串口" + portName + "配置已保存");
                }else{
                    JOptionPane.showMessageDialog(PrintDialog.this, "打印机串口未打开，无法保存配置");
                }
            }
        });

        if (LabelPrinter.getInstance().isOpen()){
            openButton.setEnabled(false);
            closeButton.setEnabled(true);
            log("打印机串口已打开");
        }else{
            openButton.setEnabled(true);
            closeButton.setEnabled(false);
            log("打印机串口未打开");
        }
    }

    // 打印日志到文本区域的方法
    private void log(String message) {
        logTextArea.append(message + "\n");
    }
}
